import axios, {AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance} from 'axios'
import store from '@/store'
import {ElMessage} from 'element-plus'

const baseURL: any = import.meta.env.VITE_BASE_URL

const service: AxiosInstance = axios.create({
    // baseURL: "http://127.0.0.1:5002",
    baseURL: "http://127.0.0.1:9999",
    timeout: 5000
})

// 请求前的统一处理
service.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        // JWT鉴权处理
        if (store.getters['user/token']) {
            // @ts-ignore
            config.headers['token'] = store.state.user.token
            console.log("测试token,{}",store.state.user.token)
        }
        return config
    },
    (error: AxiosError) => {
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    (response: AxiosResponse) => {
        const res = response.data
        if (res.code === 200) {
            return res
        } else {
            if (response.status === 200 && !res.hasOwnProperty('code')) {
                return response
            }
            showError(res)
            return Promise.reject(res)
        }
    },
    (error: AxiosError) => {
        console.log(error) // for debug
        const badMessage: any = error.message || error
        const code = parseInt(badMessage.toString().replace('Error: Request failed with status code ', ''))
        showError({code, message: badMessage})
        return Promise.reject(error)
    }
)

// 错误处理
function showError(error: any) {
    // token过期，清除本地数据，并跳转至登录页面
    if (error.code === 403) {
        // to re-login
        store.dispatch('user/loginOut')
    } else {
        ElMessage({
            message: error.msg || error.message || '服务异常',
            type: 'error',
            duration: 3 * 1000
        })
    }
}

export default service